package com.hly.musicbackground.model.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Enum.UserEnum;
import com.hly.musicbackground.Enum.UserLogEnum;
import com.hly.musicbackground.Enum.UserRootEnum;
import com.hly.musicbackground.Final.SongLinkFinal;
import com.hly.musicbackground.Utils.*;
import com.hly.musicbackground.dao.UserDao;
import com.hly.musicbackground.domain.User;
import com.hly.musicbackground.model.music.service.SongLinkService;
import com.hly.musicbackground.model.music.service.SongSheetService;
import com.hly.musicbackground.model.user.VO.UserInfoVO;
import com.hly.musicbackground.model.user.params.UserUpdateInfoParams;
import com.hly.musicbackground.model.user.service.UserLogService;
import com.hly.musicbackground.model.user.service.UserService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    private final MD5Utils md5Utils;
    private final DozerUtils dozerUtils;
    private final Base64Utils base64Utils;
    private final UserLogService userLogService;

    private final SongSheetService songSheetService;
    private final SongLinkService songLinkService;


    @Override
    public String getHeadImage(String mail) {
        User user = baseMapper.getHeadImage(mail);
        if (user == null){
            return null;
        }
        return user.getUserImage();
    }

    @Override
    public UserInfoVO getUserInfo(int id) {
        UserInfoVO convert = dozerUtils.convert(getById(id), UserInfoVO.class);
        convert.setSongSheetNum(songSheetService.countList(id));
        convert.setLikeNum(songLinkService.countList(SongLinkFinal.LIKE, id));
        convert.setRootName(UserRootEnum.findEnumByCode(convert.getRoot()).getStr());
        return convert;
    }

    @Override
    public User getUserInfo(String mail) {
        return lambdaQuery().eq(User::getMail, mail).one();
    }

    @Override
    public User getUserInfo(int id, String password) {
        return lambdaQuery().eq(User::getUserId, id)
                .eq(User::getPassword, md5Utils.encrypt(password)).one();
    }

    @Override
    public User getUserInfo(String mail, String password) {
        return lambdaQuery().eq(User::getMail, mail)
                .eq(User::getPassword, md5Utils.encrypt(password)).one();
    }

    @Override
    public void updateInfo(int id, UserUpdateInfoParams params) {
        User user = getById(id);
        if (StringUtils.isNotEmpty(params.getHeadImage())){
            user.setUserImage(params.getHeadImage());
        }
        if (StringUtils.isNotEmpty(params.getHeadImageBase64())){
            String replace = "\\" + user.getMail()
                    .replace("@", "");
            String s = base64Utils.GenerateImage(params.getHeadImageBase64(),
                    replace + UserEnum.HEAD_IMAGE.getValue());
            user.setUserImage(s);
        }
        if (StringUtils.isNotEmpty(params.getUserName())){
            user.setUserName(params.getUserName());
        }

        userLogService.addUserLogOrFail(UserLogEnum.UPDATE_USER_INFO);

        updateById(user);
    }

    @Override
    public void setUserRoot(int userId, int root) {
        if (RequestContextUtils.getUser().getUserId() == userId)
            Code.BUSINESS_ERR("非法修改");
        User byId = getById(userId);
        if (byId == null)
            Code.BUSINESS_ERR("用户不存在");
        byId.setRoot(root);
        updateById(byId);
        userLogService.addUserLogOrFail(userId, UserLogEnum.SET_ROOT, "");
    }

    public UserInfoVO getDetail(int uid) {
        UserInfoVO userInfo = getUserInfo(uid);
        return userInfo;
    }
}
